package com.pet.server.dao.common;

import java.util.List;

import com.pet.framework.page.Page;
import com.pet.server.po.AnswerInfo;
import com.pet.server.po.Article;
import com.pet.server.po.Option;
import com.pet.server.po.RequestInfo;
import com.pet.server.po.UserArticle;
import com.pet.server.po.UserInfo;
import com.pet.server.po.UserTask;
import com.pet.server.po.UserTaskDaily;
import com.pet.system.statement.Statement;

public class UserDao extends CommonDao {

	public void searchUserInfo(Page page,UserInfo user){
		Statement stms = stmsFactory.createStatement(" from UserInfo u where 1=1 and  state!=4");
		          stms.addOrderBy("u.userId desc");
		search(stms, page);
	}
	public void searchPetInfo(Page page){
		Statement stms = stmsFactory.createStatement("select p,u from PetInfo p,UserInfo u where p.userId=u.userId");
		          stms.addParam("p.petId desc");
		search(stms, page);
	}
	
	public void searchUserArticle(Page page, UserArticle userArticle) {
		Statement stms = stmsFactory.createStatement(" from UserArticle a where 1=1 and  state!=4");
		 if(null!=userArticle){
	     	  stms.append("and", "a.title", "=", userArticle.getTitle());
	      }
        stms.addOrderBy("a.createDate desc");
        search(stms, page);
	}
	
	
	public void searchRequestInfo(Page page, RequestInfo requestInfo) {
		Statement stms = stmsFactory.createStatement(" from RequestInfo a where 1=1 and  state!=4");
		 if(null!=requestInfo){
			 stms.appendLike(null, "and", "a.user_id=", requestInfo.getUserId());
	      }
       stms.addOrderBy("a.createTime desc");
       search(stms, page);
	}
	
	/**
	 * 提问
	 * @param id
	 * @return
	 */
	public List<AnswerInfo> queryeAnswerInfo(Integer id) {
	              
	     Statement stms = stmsFactory.createNativeStatement("SELECT p.* ,t.nick_name FROM answer_info p LEFT JOIN user_info t ON p.user_id=t.user_id WHERE 1=1 AND p.answer_type='request' AND p.oid=?");         
	    		 stms.addParam(id);
	    		 stms.addEntity("p", AnswerInfo.class);
	    		 
      return query(stms);
	}
	/**
	 * 调查
	 * @param id
	 * @return
	 */
	public List<Option> queryeOption(Integer id) {
		//Statement stms = stmsFactory.createStatement(" from Option a where a.requestId=?");
		
		Statement stms = stmsFactory.createNativeStatement("SELECT p.* FROM `option` p WHERE 1=1 and request_id=?");         
		stms.addParam(id);
		stms.addEntity("p", Option.class);
		
		return query(stms);
	}
	
	
	public void searchUserTask(Page page, UserTask userTask) {
		Statement stms = stmsFactory.createStatement(" from UserTask a where 1=1 and  state!=4");
		 if(null!=userTask){
			 stms.appendLike(null, "and", "a.userId=", userTask.getUserId());
	      }
		 stms.addOrderBy("a.createdTime desc");
		 search(stms, page);
	}
	
	
	public void searchUserTaskDaily(Page page, UserTaskDaily userTaskDaily) {
		Statement stms = stmsFactory.createStatement(" from UserTaskDaily a where 1=1 and  state!=4");
		 if(null!=userTaskDaily){
			 stms.appendLike(null, "and", "a.userId=", userTaskDaily.getUserId());
	      }
		 stms.addOrderBy("a.createDate desc");
		 search(stms, page);
	}
}
